package com.kaifamiao.base.Demo210815;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author lucien-ma
 * @date 2021/9/16
 * @apiNote
 */
public class Demo05 {

    public static <E extends Comparable> void sort(E[] stus) {
        for (int i = 0; i < stus.length; i++) {
            int minIndex = i;
            for (int j = i; j < stus.length; j++) {
                if (stus[minIndex].compareTo(stus[j]) > 0) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                E temp = stus[minIndex];
                stus[minIndex] = stus[i];
                stus[i] = temp;
            }
        }
    }

    public static void main(String[] args) {
        Student2 s1 = new Student2("张三", 22);
        Student2 s2 = new Student2("李四", 19);
        Student2 s3 = new Student2("王五", 24);
        Student2 s4 = new Student2("小明", 18);
        Student2 s5 = new Student2("小红", 26);
        Student2[] stus = new Student2[]{s1,s2,s3,s4,s5};
        System.out.println("排序前：");
        for (Student2 student2 : stus) {
            System.out.println(student2);
        }
        sort(stus);
        System.out.println("+++++++++++++++++++++");
        System.out.println("排序后：");
        for (Student2 student2 : stus) {
            System.out.println(student2);
        }

    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student2 implements Comparable<Student2> {
    private String name;
    private Integer age;

    @Override
    public int compareTo(Student2 o) {
        return this.age - o.age;
    }
}